package 字符串.middle;

import java.util.Arrays;

/**
 * @author Lw中
 * @date 2020/9/11 9:03
 */
public class YiWuYi {

    /**
     * 反转字符串里面的单词
     * @param s
     * @return
     */
    public static String reverseWords(String s) {
        // 将字符串中头尾部的空格去除
        s.trim();
        // 将字符串用“ ”分割成为字符串数组
        String[] s1 = s.split(" ");
        for (int i = 0; i < s1.length; i++) {
            System.out.println(s1[i]);
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < s1.length; i++) {
            // 判断字符串是不是空格组成的
            if (!s1[s1.length - i - 1].trim().isEmpty()) {
                stringBuffer.append(s1[s1.length - i - 1] + " ");
            }
        }
        return stringBuffer.toString().trim();
    }

    public static void main(String[] args) {
        String strings = reverseWords("a  good   example");
        String str = "    ";
        if (str.trim().isEmpty()) {
            System.out.println("是由空格组成");
        }
        System.out.println(strings);
    }

}
